package com.forhappy.light3ds8.db;

import java.util.LinkedList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.forhappy.light3ds8.datatype.DataBasic;
import com.forhappy.light3ds8.datatype.DataReal;

public class LedDBMgr_bak {
	private static final String TAG = "LedDBMgr";
	LedDBHelper helper;
	public LedDBMgr_bak(Context context) {
		helper = new LedDBHelper(context);
	}

	public boolean saveToDBBasic(LinkedList<DataBasic> dataBasicList){
		if(null == dataBasicList || dataBasicList.size() == 0){
			Log.e(TAG, "in LedDBMgr::saveToDB()dataBasicList is empty");
			return false;
		}
		for(DataBasic data: dataBasicList){
			SQLiteDatabase db = helper.getWritableDatabase();
			db.insert(LedDBHelper.TAB_DATAS_BASIC, null, data.toContentValue());
		}
		return true;
	}

	public boolean saveToDBReal(LinkedList<DataReal> dataRealList){
		if(null == dataRealList || dataRealList.size() == 0){
			Log.e(TAG, "in LedDBMgr::saveToDB()dataBasicList is empty");
			return false;
		}
		for(DataReal data: dataRealList){
			SQLiteDatabase db = helper.getWritableDatabase();
			db.insert(LedDBHelper.TAB_DATAS_BASIC, null, data.toContentValue());
		}

		return true;
	}

	public LinkedList<DataBasic> getDatasBasic(){
		Log.d(TAG, "enter getDatasBasicByIdx()");

		LinkedList<DataBasic> listRet = new LinkedList<DataBasic>();
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor  = db.query(LedDBHelper.TAB_DATAS_BASIC,
				new String[]{
					LedDBHelper.ITEM_ID,
					LedDBHelper.ITEM_DATAS,
					LedDBHelper.ITEM_INFO},
				null,null, null, null, LedDBHelper.ITEM_ID + " asc");
		if(null == cursor || cursor.isAfterLast()){
			Log.e(TAG, "int getRealDatasByIdx, cursor is null!");
			return listRet;
		}

		while(cursor.moveToNext()){
			listRet.add(new DataBasic(
					cursor.getInt(0),cursor.getInt(1), cursor.getString(2)));
		}
		cursor.close();
		return listRet;
	}
	public String getDatasRealByIdx(int realDataIdx){
		Log.d(TAG, "enter getDatasRealByIdx(realDataIdx:" + realDataIdx + ")");
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor  = db.query(LedDBHelper.TAB_DATAS_REAL, new String[]{ LedDBHelper.ITEM_DATAS}, LedDBHelper.ITEM_ID,
				new String[]{String.valueOf(realDataIdx)}, null, null, null);
		if(null == cursor || cursor.isAfterLast()){
			Log.e(TAG, "int getDatasRealByIdx, cursor is null!");
			return "";
		}

		if(cursor.moveToNext()){
			String datas = cursor.getString(1);
			Log.d(TAG, "get datas forIdx:" + realDataIdx + ", datas:" + datas);
			cursor.close();
			return datas;
		} else {
			Log.e(TAG, "getDatasRealByIdx moveToNext return false!");
		}
		return "";
	}
}
